package com.example.pojo;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import javax.sql.rowset.serial.SerialBlob;
import java.io.IOException;
import java.sql.Blob;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.Base64;

@AllArgsConstructor
@NoArgsConstructor
@Data
public class Interaction {
    @Id
    private Long id; //联系人的id
    private String name; //联系人的名字
    private Integer positionId; //职位id
    private Integer departmentId; //所属部门id
    //新增属性  +
    private String sender;  //发送信息的人名
    private String recipient; //接收信息的人名
    private String content; //信息内容
    private String taskContent; //任务内容
    @TableField(fill = FieldFill.INSERT)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime sendTime; //信息发送时间
    //新增属性 映射LONGBLOB字段
    @Lob
    @JsonDeserialize(using = BlobDeserializer.class)
    private Blob file; // 映射LONGBLOB字段

    private String files;

    private String fileName; //文件名称
}


class BlobDeserializer extends JsonDeserializer<Blob> {
    @Override
    public Blob deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
        String base64String = jsonParser.getText();
        byte[] bytes = Base64.getDecoder().decode(base64String);
        try {
            return new SerialBlob(bytes);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
}